// pages/index/online_answer/subject_answer/subject_answer.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        paperid: "",
        subjectAnswerList: [],
        index: 0,
        subjectIndex: {},
        show: false,
        is_checkbox: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            paperid: options.paperid
        })
        this.getSubjectAnswerList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getSubjectAnswerList: function (e) {
        const than = this
        http.get("questions/open/getAppletPaper", {
            paperid: than.data.paperid,
            applet_user_id: app.globalData.userInfo.applet_user_id
        }).then(res => {
            if (res.data.code == 200) {
                res.data.data.array.forEach((item, index) => {
                    item.answer = ""
                    item.list.forEach((item1, index1) => {
                        item1.letter = String.fromCharCode(65 + index1);
                        item1.is_answer_checkbox = false;
                        if (item1.is_answer == 1) {
                            item.answer += String.fromCharCode(65 + index1) + "　";
                        }
                    })
                    item.is_answer = false
                    item.index = index
                    item.index_show = index == 0
                })
                than.setData({
                    subjectAnswerList: res.data.data.array,
                })
                than.showSubjectIndex()
            }
        })
    },
    showSubjectIndex: function (e) {
        const than = this
        than.data.subjectAnswerList.forEach((item, index) => {
            if (item.index_show) {
                than.setData({
                    subjectIndex: than.data.subjectAnswerList[item.index]
                })
            }
        })
    },
    /**
     * 下一题单击
     * @param e
     */
    onNext: function (e) {
        const value = e.currentTarget.dataset.value
        this.data.subjectAnswerList.forEach((item, index) => {
            if (value == index) {
                item.index_show = true
            } else {
                item.index_show = false
            }
        })
        this.setData({
            subjectAnswerList: this.data.subjectAnswerList
        })
        this.showSubjectIndex()
    },
    /**
     * 上一题单击
     * @param e
     */
    onAbove: function (e) {
        const value = e.currentTarget.dataset.value
        log.e(JSON.stringify(this.data.subjectAnswerList))
        this.data.subjectAnswerList.forEach((item, index) => {
            if (value == index) {
                item.index_show = true
            } else {
                item.index_show = false
            }
        })
        this.setData({
            subjectAnswerList: this.data.subjectAnswerList
        })
        this.showSubjectIndex()
    },
    /**
     * 单击题
     * @param e
     */
    onAnswerShow: function (e) {
        const value = e.currentTarget.dataset.value
        this.data.subjectAnswerList.forEach((item, index) => {
            if (value == index) {
                item.index_show = true
            } else {
                item.index_show = false
            }
        })
        this.setData({
            subjectAnswerList: this.data.subjectAnswerList
        })
        this.showSubjectIndex()
    },
    /**
     * 答题卡单击
     * @param e
     */
    onAnswer: function (e) {
        this.setData({
            show: true
        });
    },
    onClose() {
        this.setData({
            show: false
        });
    },
    /**
     * 答题单选
     * @param e
     */
    onCircularRadio: function (e) {
        const id = e.currentTarget.dataset.id
        const index1 = e.currentTarget.dataset.index
        log.e(index1+JSON.stringify( this.data.subjectAnswerList))
        let isTrue = false
        this.data.subjectAnswerList.forEach((item, index) => {
            if(index1==index){
                item.list.forEach((item1, index1) => {
                    if (item1.is_answer_checkbox) {
                        isTrue = true;
                    }
                })
            }
        })
        if (isTrue) {
            return
        }
        this.data.subjectAnswerList.forEach((item, index) => {
            item.list.forEach((item1, index1) => {
                if (id == item1.id) {
                    item1.is_answer_checkbox = true;
                    item.is_answer = true
                } 
            })
        })
        this.setData({
            subjectAnswerList: this.data.subjectAnswerList
        })
        this.showSubjectIndex()
    },
    /**
     * 答题多选
     * @param e
     */
    onCircularCheckbox: function (e) {
        const id = e.currentTarget.dataset.id
        let isTrue = false
        this.data.subjectAnswerList.forEach((item, index) => {
            item.list.forEach((item1, index1) => {
                if (id == item1.id) {
                    if (item.is_answer) {
                        isTrue = true
                    }
                }
            })
        })
        if (isTrue) {
            return
        }
        this.data.subjectAnswerList.forEach((item, index) => {
            item.list.forEach((item1, index1) => {
                if (id == item1.id) {
                    item1.is_answer_checkbox = !item1.is_answer_checkbox;
                }
            })
        })
        this.setData({
            subjectAnswerList: this.data.subjectAnswerList
        })
        this.showSubjectIndex()
    },
    /**
     * 多选确认答案
     * @param e
     */
    onSubmitAnswer: function (e) {
        const id = e.currentTarget.dataset.id
        this.data.subjectAnswerList.forEach((item, index) => {
            if (item.id == id) {
                item.is_answer = true
            }
        })
        this.setData({
            subjectAnswerList: this.data.subjectAnswerList,
        })
        this.showSubjectIndex()
    },
    onSubmit: function (e) {
        const list = []
        this.data.subjectAnswerList.forEach((item, index) => {
            item.list.forEach((item1, index1) => {
                if (item1.is_answer_checkbox) {
                    list.push({
                        appletUserId: app.globalData.userInfo.applet_user_id,
                        nickname: app.globalData.userInfo.nickname,
                        paperid: this.data.paperid,
                        questionsid: item.id,
                        optionid: item1.id,
                        answer: item1.letter,
                    })
                }
            })
        })
        log.e(JSON.stringify(this.data.subjectAnswerList))
        let yes_total = 0
        let wrong_total = 0
        this.data.subjectAnswerList.forEach((item, index) => {
            if (item.is_radio == 1) {
                item.list.forEach((item1, index1) => {
                    if (item1.is_answer == 1 && item1.is_answer_checkbox == true) {
                        yes_total++;
                    }
                })
            } else if (item.is_radio == 2) {
                let is = ""
                item.list.forEach((item1, index1) => {
                    if (item1.is_answer == 1 && item1.is_answer_checkbox == true) {
                        is += item1.letter + "　"
                    }
                    if (item.answer == is) {
                        yes_total++;
                    }
                })
            }
        })
        log.e(yes_total)
       
        Dialog.confirm({
            title: '提示',
            message: '确认提交吗？',
        })
            .then(() => {
                http.putJson("questions/open/addAnswerLog", {
                    id: this.data.paperid,
                    total: this.data.subjectAnswerList.length,
                    yes_total: yes_total,
                    wrong_total: this.data.subjectAnswerList.length - yes_total,
                    nickname: app.globalData.userInfo.nickname,
                    applet_user_id: app.globalData.userInfo.applet_user_id,
                    area_code: app.globalData.areaCode,
                    questionnaire_type:2
                }).then(res => {
        
                })
                http.putJson("questions/open/addQuesAnswer", list).then(res => {
                    if (res.data.code == 200) {
                        Toast.success(res.data.msg);
                        setTimeout(res => {
                            wx.navigateBack({
                                delta: 2
                            })
                        }, 1000)
                    } else {
                        Toast.fail(res.data.msg);
                    }
                })
            })
            .catch(() => {
                // on cancel
            });
    }
})